package com.example.easeimpro.model.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.easeimpro.model.bean.UserInfo;
import com.example.easeimpro.model.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

public class ContactTableDao {
        private DBHelper mHelper;
        private SQLiteDatabase db;
        public ContactTableDao(DBHelper helper){
                mHelper = helper;
                db = helper.getReadableDatabase();
        }
        //获取所有联系人
        @SuppressLint("Range")
        public List<UserInfo> getContacts(){
                String sql = "select * from "+ContactTable.TAB_NAME+" where "+ContactTable.COL_IS_CONTACT + "=1";
                Cursor cursor = db.rawQuery(sql, null);//执行语句
                List<UserInfo> list = new ArrayList<>();
                while (cursor.moveToNext()){
                        UserInfo info = new UserInfo();
                        info.setHxid(cursor.getString(cursor.getColumnIndex(ContactTable.COL_HXID)));
                        info.setName(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NAME)));
                        info.setNick(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NICK)));
                        info.setPhoto(cursor.getString(cursor.getColumnIndex(ContactTable.COL_PHOTO)));
                list.add(info);
                }

                //关闭资源
                cursor.close();
                //返回联系人列表
                return list;

        }

        @SuppressLint("Range")
        public UserInfo getContactByHx(String hxId){

                if(hxId == null){
                        return null;
                }
                //数据库连接

                //执行查询语句
                String sql = "select * from "+ContactTable.TAB_NAME +" where "+ContactTable.COL_HXID+"=?";
                Cursor cursor = db.rawQuery(sql, new String[]{hxId});
                UserInfo userInfo = null;
                if(cursor.moveToNext()){
                         userInfo = new UserInfo();
                        userInfo.setHxid(cursor.getColumnName(cursor.getColumnIndex(ContactTable.COL_HXID)));
                        userInfo.setName(cursor.getColumnName(cursor.getColumnIndex(ContactTable.COL_NAME)));
                        userInfo.setNick(cursor.getColumnName(cursor.getColumnIndex(ContactTable.COL_NICK)));
                        userInfo.setPhoto(cursor.getColumnName(cursor.getColumnIndex(ContactTable.COL_PHOTO)));
                }
                //关闭资源
                cursor.close();
                return userInfo;

        }
        //通过环信id获取用户联系人信息
        public List<UserInfo> getContactsByHx(List<String> hxIds){
                if(hxIds == null || hxIds.size()<=0){
                        return null;
                }
                List<UserInfo> contacts = new ArrayList<>();
                //遍历hxList 来查找
                for(String hxid: hxIds){
                        UserInfo contact = getContactByHx(hxid);
                        contacts.add(contact);
                }
                return contacts;
        }
//保存单个联系人
                public void saveContact(UserInfo user,boolean isMyContact){
                if(user == null){
                        return;
                }
                //执行保存语句
                ContentValues values = new ContentValues();
                values.put(ContactTable.COL_HXID,user.getHxId());
                values.put(ContactTable.COL_NAME,user.getName());
                values.put(ContactTable.COL_NICK,user.getNick());
                values.put(ContactTable.COL_PHOTO,user.getPhoto());
                values.put(ContactTable.COL_IS_CONTACT,isMyContact? 1 : 0);
                db.replace(ContactTable.TAB_NAME,null,values);
                //释放资源


        }
        //保存联系人信息
        public void saveContacts(List<UserInfo> contacts,boolean isMyContact){
                if(contacts == null || contacts.size() <= 0){
                        return;
                }
                for (UserInfo contact : contacts){
                        saveContact(contact,isMyContact);
                }
        }

        public void deleteContactByHxId(String hxId){
                if(hxId == null ){
                        return;
                }
               db.delete(ContactTable.TAB_NAME,ContactTable.COL_HXID+" = ?",new String[]{hxId});
        }
}
